package services

import (
	"fmt"
	"gohub/pkg/helpers"
	"math/rand"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

type MonthData struct {
	Time   string `json:"time"`
	Amount string `json:"amount"`
}

func GetTrend() (map[string]interface{}, error) {
	// 基础金额生成（保持与示例相似范围）
	baseAmount := 280000 + rand.Float64()*10000
	todayAdded := 800 + rand.Float64()*200
	todaySub := 700 + rand.Float64()*200

	// 生成月度数据（6个月）
	monthData := make([]MonthData, 6)
	for i := 0; i < 6; i++ {
		month := time.Now().AddDate(0, -i, 0).Format("2006-01")
		amount := fmt.Sprintf("%.2f", 50+rand.Float64()*100)
		monthData[5-i] = MonthData{Time: month, Amount: amount}
	}

	// 生成每日曲线数据（与月度数据同时间段）
	dailyData := make([]MonthData, 6)
	for i, md := range monthData {
		amount := fmt.Sprintf("%.2f", rand.Float64()*150)
		dailyData[i] = MonthData{Time: md.Time, Amount: amount}
	}
	// 组合数据
	data := map[string]interface{}{
		"allAmount":       helpers.RoundFloat(baseAmount, 2),
		"todayAdded":      helpers.RoundFloat(todayAdded, 2),
		"todaySub":        helpers.RoundFloat(todaySub, 2),
		"todayAmount":     helpers.RoundFloat(todayAdded-todaySub, 2),
		"monthAmountList": monthData,
		"dailyCurve":      dailyData,
	}
	return data, nil
}

type HourlyStat struct {
	TimeStr string `json:"timeStr"`
	Income  string `json:"income"`
	Expense string `json:"expense"`
	Balance string `json:"balance"`
}

func GetAmount() (map[string]interface{}, error) {
	rand.NewSource(time.Now().UnixNano())

	timeSlots := []string{"0点", "3点", "6点", "9点", "12点", "15点", "18点", "21点"}
	stats := make([]HourlyStat, len(timeSlots))

	for i, timeStr := range timeSlots {
		income := helpers.RandomAmount(1, 10)
		expense := helpers.RandomAmount(1, 10)
		balance := income - expense

		stats[i] = HourlyStat{
			TimeStr: timeStr,
			Income:  helpers.FormatMoney(income),
			Expense: helpers.FormatMoney(expense),
			Balance: helpers.FormatMoney(balance),
		}
	}

	data := map[string]interface{}{
		"result": stats,
	}
	return data, nil
}

type DailyStat struct {
	Date   string `json:"date"`
	Amount string `json:"amount"`
}

func generateRandomAmount() string {
	// 生成-10.00到40.00之间的随机金额
	value := rand.Float64()*50 - 10
	return strconv.FormatFloat(value, 'f', 2, 64)
}

func GetCalendar() (map[string]interface{}, error) {
	now := time.Now()
	startDate := now.AddDate(0, -2, -now.Day()+1) // 3个月前第一天
	endDate := now                                // 今天

	var stats []DailyStat
	for d := startDate; !d.After(endDate); d = d.AddDate(0, 0, 1) {
		stats = append(stats, DailyStat{
			Date:   d.Format("2006-01-02"),
			Amount: generateRandomAmount(),
		})
	}

	data := map[string]interface{}{
		"result": stats,
	}
	return data, nil
}

type areaStat struct {
	Name  string `json:"name"`
	Value int    `json:"value"`
}

func GetPie(c *gin.Context) ([]areaStat, error) {
	areaSlots := []string{"华北", "东北", "华东", "中南", "西南", "西北"}
	stats := make([]areaStat, len(areaSlots))

	for i, area := range areaSlots {
		stats[i] = areaStat{
			Name:  area,
			Value: helpers.RandomInt(100, 3999),
		}
	}

	return stats, nil
}

func GetWordcloud(c *gin.Context) (map[string]interface{}, error) {
	data := map[string]interface{}{}
	return data, nil
}

func GetBmap(c *gin.Context) (map[string]interface{}, error) {
	data := map[string]interface{}{}
	return data, nil
}

func GetRegions(c *gin.Context) (map[string]interface{}, error) {
	data := map[string]interface{}{}
	return data, nil
}

func GetSheets(c *gin.Context) (map[string]interface{}, error) {
	data := map[string]interface{}{}
	return data, nil
}
